
import { store } from '@/stores';
import { getDictList, getDictSearch, getDictTree } from '@/utils/net';
import { defineStore } from 'pinia';

interface DistState {
    dictMapList: {},
    dictMapObj: {},
    dictMapTree: {}
}

export const useDistStore = defineStore({
    id: 'app-dist',
    state: (): DistState => ({
        dictMapObj: {},
        dictMapList: {},
        dictMapTree: {}
    }),
    getters: {
    },
    actions: {
        getDictList(zdObj: any): Promise<Array<any>> {
            return new Promise((resolve, reject) => {
                if (this.dictMapList[zdObj.zdlb]) {
                    resolve(this.dictMapList[zdObj.zdlb])
                } else {
                    getDictList(zdObj).then(result => {
                        let list = []
                        for (let i in result.data.data) {
                            list.push(result.data.data[i])
                        }
                        this.dictMapList[zdObj.zdlb] = list;
                        this.dictMapObj[zdObj.zdlb] = result.data.data;
                        resolve(list);
                    })
                }
            })
        },
        getDictListByObj(zdObj: any) {
            return new Promise((resolve, reject) => {
                if (this.dictMapObj[zdObj.zdlb]) {
                    resolve(this.dictMapObj[zdObj.zdlb])
                } else {
                    getDictList(zdObj).then(result => {
                        let list = []
                        for (let i in result.data.data) {
                            list.push(result.data.data[i])
                        }
                        this.dictMapList[zdObj.zdlb] = list;
                        this.dictMapObj[zdObj.zdlb] = result.data.data;
                        resolve(result.data.data);
                    })
                }
            })
        },
        getDictTree(zdObj: any) {
            return new Promise((resolve, reject) => {
                if (this.dictMapTree[zdObj.zdlb]) {
                    resolve(this.dictMapTree[zdObj.zdlb])
                } else {
                    getDictTree(zdObj).then(result => {
                        this.dictMapTree[zdObj.zdlb] = result.data.data;
                        resolve(result.data.data);
                    })
                }
            })
        },
        getDictMc(zdlb: string, dm: string) {
            return new Promise((resolve, reject) => {
                this.getDictListByObj({ zdlb: zdlb }).then((result: any) => {
                    if (result[dm] && result[dm].mc) {
                        resolve(result[dm].mc)
                    } else {
                        resolve(dm)
                    }
                })
            })
        },
        getDictSearch(zdlb: string, pageNumber: number, searchKey: string) {
            return new Promise((resolve, reject) => {
                getDictSearch({ zdlb: zdlb }, { pageSize: 10, totalRequired: true, pageNumber: pageNumber }, searchKey).then(result => {
                    resolve(result.data.data)
                })
            })
        },
        getDictObj(zdlb:string,dm:string){

        }
    },
})

// Need to be used outside the setup
export function useDistStoreWithOut() {
    return useDistStore(store);
}
